$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tbuser/list',
        datatype: "json",
        colModel: [
            { label: '用户ID', name: 'userId', index: "userId", width: 45, key: true },
            { label: '用户名', name: 'username', width: 75 },
            { label: '昵称', name: 'nickname', width: 75 },
            { label: '邮箱', name: 'email', width: 90 },
            { label: '手机号', name: 'mobile', width: 80 },
            { label: '状态', name: 'identity', width: 80, formatter: function(value, options, row){
                return value === 0 ?
                    '<span class="label label-danger">用户</span>' :
                    '<span class="label label-success">作者</span>';
            }},
            { label: '创建时间', name: 'createTime', index: "createTime", width: 90}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.records",
            page: "page.current",
            total: "page.pages",
            records: "page.total"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            username: null
        },
        showList: true,
        showList1:true,
        title:null,
        // roleList:{},
        tbuser:{
            status:1,
            deptId:null,
            deptName:null,
            roleIdList:[]
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        update: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getUser(userId);

        },
        add: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }
            vm.showList = true;
            vm.showList1 = false;
            vm.title = "成为作者";
            vm.getUser(userId);

        },
        del: function () {
            var userIds = getSelectedRows();
            if(userIds == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/tbuser/delete",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        update2: function () {
            var url = "sys/tbuser/update2";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.tbuser),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        author: function () {
            var url = "sys/tbuser/beauthor";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.tbuser),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getUser: function(userId){
            $.get(baseURL + "sys/tbuser/findById/"+userId, function(r){
                vm.tbuser = r.tbuser;
                vm.tbuser.password = null;

            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.username},
                page:page
            }).trigger("reloadGrid");
        }
    }
});
